package com.bwt.usermanage;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.bwt.usermanage.Entity.User;
import com.bwt.usermanage.Mapper.UserDao;
import com.bwt.usermanage.adapter.UserAdapter;
import com.bwt.usermanage.utils.DialogUtil;


import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
    ListView searchLv;
    EditText searchEt;
    TextView emptyTv;
    List<User>mDatas;   //数据源
    UserAdapter adapter;    //适配器对象
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        mDatas = new ArrayList<>();
        adapter = new UserAdapter(this,mDatas);
        searchLv.setAdapter(adapter);

        searchLv.setEmptyView(emptyTv);   //设置无数局时，显示的控件

        setLVLongClickListener();
        setLVOnItemClickListener();
        userDao = AppDatabase.getInstance(this).userDao();
    }

    private void initView() {
        searchEt = findViewById(R.id.search_et);
        searchLv = findViewById(R.id.search_lv);
        emptyTv = findViewById(R.id.search_tv_empty);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_iv_back:
                finish();
                break;
            case R.id.search_iv_sh:   //执行搜索的操作
                String msg = searchEt.getText().toString().trim();
//                判断输入内容是否为空，如果为空，就提示不能搜索
                if (TextUtils.isEmpty(msg)) {
                    Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                //开始搜索
                List<User> list = userDao.findUserByborrowerNameOrSFZ(msg);
                mDatas.clear();
                mDatas.addAll(list);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    private void setLVOnItemClickListener() {

        searchLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                User clickBean = mDatas.get(position);//获取正在被点击的这条信息
                Bundle bundle = new Bundle();
                bundle.putInt("uid", clickBean.uid);

                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(SearchActivity.this, InsertOrEditActivity.class);
                startActivity(intent);
            }
        });

    }

    /** 设置ListView的长按事件*/
    private void setLVLongClickListener() {
        searchLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                User clickBean = mDatas.get(position);//获取正在被点击的这条信息

                //弹出提示用户是否删除的对话框
                showDeleteItemDialog(clickBean);
                return false;
            }
        });
    }


    private void showDeleteItemDialog(final User clickBean) {
        DialogUtil.showMsgDialog(this, "提示信息", "您确定要删除这条记录么？", null, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //执行删除的操作
                userDao.delete(clickBean);
                mDatas.remove(clickBean);   //实时刷新，移除集合当中的对象
                adapter.notifyDataSetChanged();   //提示适配器更新数据
            }
        });

    }


}
